package com.example.demo.controller;

import com.example.demo.pojo.Address;
import com.example.demo.pojo.Result;
import com.example.demo.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Validated
public class AddressController {
	@Autowired
	private IAddressService addressService;

	@PostMapping("/save")
	public Result save(@RequestBody Address address)
	{
		addressService.save(address);
		return Result.success();
	}

	@PutMapping("/update")
	public Result update(@RequestBody Address address)
	{
		addressService.update(address);
		return Result.success();
	}
	@GetMapping("/list/{userID}")
	public Result findAll(@PathVariable Integer userID) {
		List<Address> list=addressService.findAll(userID);
		return Result.success(list);
	}
	@GetMapping("/listSingle/{userID}/{addressID}")
	public Result findSingle(@PathVariable Integer userID,@PathVariable Integer addressID) {
		List<Address> list=addressService.findSingle(userID,addressID);
		return Result.success(list);
	}
	@GetMapping("/listDefault/{userID}")
	public Result findDefault(@PathVariable Integer userID) {
		List<Address> list=addressService.findDefault(userID);
		return Result.success(list);
	}
	@DeleteMapping("/delete/{addressID}")
	public void remove(@PathVariable Integer addressID) {
		addressService.removeAddressItem(addressID);
	}
}
